package filesystem.adapters;

import javax.swing.JLabel;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;

/**
 * Adapter that will adapt one single property of the {@link EObject} to
 * {@link JLabel} text.
 * 
 * @author Renat Zubairov, Igor Drobiazko
 */
public class JLabelPropertyAdapter extends AbstractAdapter {

	JLabel label;
	
	/**
	 * Constructor with {@link JLabel} as parameter. Shows which {@link JLabel}
	 * will be updated as soon as value changed.
	 * 
	 * @param label -
	 *            {@link JLabel} to updated
	 * @param featureId -
	 *            ID of the feature to display as {@link JLabel} text
	 */
	public JLabelPropertyAdapter(JLabel label, int featureId) {
		super(featureId);
		this.label = label;
	}

	/**
	 * This method updates label text
	 * 
	 * @param newTarget
	 */
	protected void update(EObject object) {
		// Get the "feature" - analogy of "field" in reflection
		EStructuralFeature feature = object.eClass().getEStructuralFeature(featureId);
		if (feature != null) {
			// Use reflective access read the feature value
			Object value = object.eGet(feature);
			label.setText(String.valueOf(value));
		}
	}
	

	/**
	 * This method is called when adapter removed from it's target
	 */
	public void unsetTarget(Notifier oldTarget) {
		super.unsetTarget(oldTarget);
		// Cleanup the label
		label.setText("");
	}

}
